$.views.converters({
	fgoodsStatus : function(value) {
		if(!value){
			return "";
		}
		for(index in stats_goodsStatus){
			var goodsStatus = stats_goodsStatus[index];
			if(goodsStatus.dictcode == value){
				return goodsStatus.dictname;
			}
		}
		return  "未知类型("+value+")";
	},

	fapplyStatus : function(value){
		if(!value){
			return "";
		}
		for(index in stats_approves){
			var approves = stats_approves[index];
			if(approves.dictcode == value){
				return approves.dictname;
			}
		}
		return  "未知类型("+value+")";
	},
	ftime : function(value){
		if(!!value){
			return value.year+"-"+value.monthValue+"-"+value.dayOfMonth+" "+value.hour+":"+value.minute+":"+value.second;
		}
		return "";
	},
	fmoneyUnit : function(value){
		if(!value){
			return "";
		}
		for(index in stats_currencys){
			var currencys = stats_currencys[index];
			if(currencys.dictcode == value){
				return currencys.dictname;
			}
		}
		return  "未知类型("+value+")";
	}
});
var goodsStatsCtrl  = new XFApp.client();
XFApp.client.prototype.goodsStats = {
	successCode : "10000",
	page : null,
	hisPage : null,
	hisId : 0,
	cusPage : null,
	urlItems : {
		queryByPageURL : basePath+"/packagestats/goodsStats/queryPage",
		delGoodsRUL : basePath +"/packagestats/goodsRecord/delGoods",
		queryCustomerByPageURL : basePath + "/packagestats/goodsRecord/queryCusPage",
		queryByHisPageURL : basePath + "/packagestats/goodsStats/queryHistoryPage",
		importDataURL:basePath+"/nt/packageForstats/goodsRecord/importGoods",//导入备案 @ps
	},
	
	queryPage : function(){
		_this = this;
		var params = {
			itemno : $("#itemno").val(),
			itemname : $("#itemname").val(),
			customerCode : $("#customercode").val()
		}
		if(!!_this.page){
			_this.page.page("remote",0,params);
		}else{
			_this.page = $("#page").page({
				pageSize : 10,
				remote: {
					url: _this.urlItems.queryByPageURL,
			        params: params, 
			        success: function (data, pageIndex) {
			        	if(data.code==_this.successCode){
			        		var pageResult = data.result;
			        		if(pageResult.itemList.length>0){
			        			for(var index in pageResult.itemList){
			        				var element = pageResult.itemList[index];
			        				element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
			        			}
			        			$("#page_grid").html($("#dataTmp").render(pageResult.itemList));
			        		}else{
			        			$("#page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
			        		}
			        	}else{
			        		  $.alert({
			        	  	        title: '温馨提示',
			        	  	        content: '数据查询失败!',
			        	  	        confirmButtonClass: 'btn-info',
			        	  	        confirmButton: '确定',
			        	  	        columnClass: 'col-md-4 col-md-offset-4'
			        	  	    });
			        	}
			        },
			        error : function(){
			      	  $.alert({
			    	        title: '温馨提示',
			    	        content: '数据查询失败!',
			    	        confirmButtonClass: 'btn-info',
			    	        confirmButton: '确定',
			    	        columnClass: 'col-md-4 col-md-offset-4'
			    	    });
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页,{total}项'
			});
		}
	},
	queryHisPage : function(id){
		_this = this;
		var params = {
			id :id
		}
		if(!!_this.hisPage){
			_this.hisPage.page("remote",0,params);
		}else{
			_this.hisPage = $("#history_page").page({
				pageSize : 5,
				remote: {
					url: _this.urlItems.queryByHisPageURL,
			        params: params, 
			        success: function (data, pageIndex) {
			        	if(data.code==_this.successCode){
			        		var pageResult = data.result;
			        		if(pageResult.itemList.length>0){
			        			for(var index in pageResult.itemList){
			        				var element = pageResult.itemList[index];
			        				element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
			        			}
			        			$("#his_page_grid").html($("#hisDataTmp").render(pageResult.itemList));
			        		}else{
			        			$("#his_page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
			        		}
			        	}else{
			        		  $.alert({
			        	  	        title: '温馨提示',
			        	  	        content: '数据查询失败!',
			        	  	        confirmButtonClass: 'btn-info',
			        	  	        confirmButton: '确定',
			        	  	        columnClass: 'col-md-4 col-md-offset-4'
			        	  	    });
			        	}
			        },
			        error : function(){
			      	  $.alert({
			    	        title: '温馨提示',
			    	        content: '数据查询失败!',
			    	        confirmButtonClass: 'btn-info',
			    	        confirmButton: '确定',
			    	        columnClass: 'col-md-4 col-md-offset-4'
			    	    });
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页,{total}项'
			});
			_this.hisId = id;
		}
	},
	delGoods : function(id){
		$.confirm({
		    title: '温馨提示',
		    content: '是否确认删除？',
		    columnClass: 'col-md-4 col-md-offset-4',
		    confirmButtonClass: 'btn-info',
	  	    confirmButton: '确定',
	  	    cancelButton: '取消',
		    confirm: function(){
		    	$.ajax({
					url:_this.urlItems.delGoodsRUL,
					data:{id:id},//JSON.stringify(docket),
					dataType:'json',
					type:'post',
					success:function(data){
						if(data.code!=10000){
							  $.alert({
			        	  	        title: '温馨提示',
			        	  	        content: data.msg,
			        	  	        confirmButtonClass: 'btn-info',
			        	  	        confirmButton: '确定',
			        	  	        columnClass: 'col-md-4 col-md-offset-4'
			        	  	    });
						}else{
							_this.queryPage();
						}
					},
					error:function(){
						  $.alert({
		      	  	        title: '温馨提示',
		      	  	        content: "删除出现异常,请联系开发人员",
		      	  	        confirmButtonClass: 'btn-info',
		      	  	        confirmButton: '确定',
		      	  	        columnClass: 'col-md-4 col-md-offset-4'
		      	  	    });
					}
				});
		    }
		    
		});
	},
	queryCusPage : function(){
		_this = this;
		var params = {
			docketCode : $("#customercodeFilter").val(),
			docketName : $("#customernameFilter").val()
		}
		
		if(!!_this.cusPage){
			_this.cusPage.page("remote",0,params);
		}else{
			
			_this.cusPage = $("#customer_page").page({
				pageSize : 5,
				remote: {
					url: _this.urlItems.queryCustomerByPageURL,
			        params: params, 
			        success: function (data, pageIndex) {
			        	if(data.code==_this.successCode){
			        		var pageResult = data.result;
			        		$("#customer_data_grid").html($("#cusDataTmp").render(pageResult.itemList));
			        	}else{
			        		  $.alert({
			        	  	        title: '温馨提示',
			        	  	        content: '数据查询失败!',
			        	  	        confirmButtonClass: 'btn-info',
			        	  	        confirmButton: '确定',
			        	  	        columnClass: 'col-md-4 col-md-offset-4'
			        	  	    });
			        	}
			        },
			        error : function(){
			      	  $.alert({
			    	        title: '温馨提示',
			    	        content: '数据查询失败!',
			    	        confirmButtonClass: 'btn-info',
			    	        confirmButton: '确定',
			    	        columnClass: 'col-md-4 col-md-offset-4'
			    	    });
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页'
			});
		}
	},
	selCustomer : function(value){
		$("#customercode").val(value);
		$("#choose_customer_modal").modal('hide'); 
	},
	queryHistory : function(id){
		_this.queryHisPage(id);
		$("#choose_history_modal").modal('show'); 
	},
	importData:function (){
		var _this = this;
		var params = [];
		$("input").each(function(){
			if(!!$(this).val() && !!$(this).attr("id")){
				var param = {name:'',value:''};
				param.name=$(this).attr("id");
				param.value=$(this).val();
				params.push(param);
			}
		});
		webDialogUtil.simpleConfrimAlert("是否确认提交", function(){
			//基于ajaxfileUpload.js实现
			$.ajaxFileUpload({
				url : _this.urlItems.importDataURL,
				secureuri : false,
				fileElementId : 'excelFile',//file标签的id
				dataType : 'json',
				data : params,
				success: function (data, status) {
					webDialogUtil.simpleAlert(data.msg, "温馨提示", null, 300, 10);
				},
				error: function (data, status, e) {
					webDialogUtil.simpleAlert("处理异常", "温馨提示");
				}
			});
		});

	},
	event : function(){
		_this = this;
		$("#goods_search").on("click",function(){
			_this.queryPage();
		});
		$("#customercode_choose").on("click",function(){
			$("#choose_customer_modal").modal();
		});
		$("#custom_serach").on("click",function(){
			_this.queryCusPage();
		});
		
	
		$("#custom_reset").on("click",function(){
			$("input[name='customercodeFilter']").val("");
			$("input[name='customernameFilter']").val("");
		});

		$("#importButton").on("click",function(){
			this.importData();
		});
	},
};
$(function(){
	goodsStatsCtrl.goodsStats.event();
	goodsStatsCtrl.goodsStats.queryPage();
});
